
#ifdef __WIN32
#include <winsock2.h>
#include <conio.h>
#else
typedef unsigned char u_char;
#include <arpa/inet.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define ERR_OUT(X)\
do {\
	printf("ERROR! ");\
	printf X;\
	goto Err;\
} while(0)\

#define DATA_LEN_MAX		(1024*512)

static const char g_copyright[] = "create by liujianhua, yuxing software.";

static int copyright(void)
{
	const char *p = g_copyright;
	if (p[0] != 'c' || p[1] != 'r' || p[2] != 'e' || p[3] != 'a' || p[4] != 't' || p[5] != 'e' ||  p[6] != ' ' || 
		p[7] != 'b' || p[8] != 'y' || p[9] != ' ' || 
		p[10] != 'l' || p[11] != 'i' || p[12] != 'u' || p[13] != 'j' || p[14] != 'i' || p[15] != 'a' ||  p[16] != 'n' || p[17] != 'h' || p[18] != 'u' || p[19] != 'a' || p[20] != ',' || p[21] != ' ' || 
		p[22] != 'y' || p[23] != 'u' || p[24] != 'x' || p[25] != 'i' || p[26] != 'n' || p[27] != 'g' ||  p[28] != ' ' || 
		p[29] != 's' || p[30] != 'o' || p[31] != 'f' || p[32] != 't' || p[33] != 'w' || p[34] != 'a' || p[35] != 'r' || p[36] != 'e' ||  p[37] != '.')
		return -1;
	printf("%s\n", g_copyright);
	printf("All Rights Reserved.\n");

	return 0;
}

int main(int argc, char* argv[])
{
	FILE*					fp = NULL;
	FILE*					ts = NULL;

	u_char *data_buf;
	int	file_off, data_off, data_len;
	u_char *p, *buf;
	int len, i;
    int rtp = 0;

	data_buf = (u_char *)malloc(DATA_LEN_MAX);
	if(data_buf == NULL)
		ERR_OUT(("malloc error\n"));
	data_len = 0;

	if(copyright( ))
		return -1;
#if 1
	if (argc != 2 && argc != 3)
		ERR_OUT(("readptcp <filename> [rtp head length]\n"));
	fp = fopen(argv[1], "rb");
    if (argc == 3)
        rtp = atoi(argv[2]);
#else
	fp = fopen("G:\\readcap\\1.raw", "rb");
#endif
	if (fp == NULL)
		ERR_OUT(("fopen raw error\n"));

	ts = fopen("pcap.ts", "wb");
	if (ts == NULL)
		ERR_OUT(("fopen ts error\n"));

	data_off = 0;
	data_len = 0;
	file_off = 0;
	while (!feof(fp)) {
Read:
		//read raw
		if (data_off > 0 && data_len > 0)
			memmove(data_buf, data_buf + data_off, data_len);
		data_off = 0;
		len = fread(data_buf + data_len, 1, DATA_LEN_MAX - data_len, fp);
		if(len <= 0)
			ERR_OUT(("press anly key to exit\n"));
		file_off += len;
		data_len += len;

		while(data_len >= 1024) {
			#define DATA_SKIP(LEN)	data_off += LEN, data_len -= LEN
	
			//TCP to TS
			p = data_buf + data_off;
			for (i = 0; i < data_len; i ++) {
				if (p[i] == '$' && p[i + 1] == 0)
					break;
			}
			if (i > 0 && p[0] != 'R')
				printf("warn! skip %d, %d, %c, %02x %02x %02x %02x\n", i, file_off - data_len, p[0], p[0], p[1], p[2], p[3]);
			if (i > 0) {
				DATA_SKIP(i);
			}
			if (data_len <= 4)
				continue;

			p = data_buf + data_off;
			{
				u_short length;
	
				memcpy(&length, p + 2, 2);
				len = ntohs(length);
				if(len < 0 || len > 1024 * 4) {
					DATA_SKIP(1);
					continue;
				}
			}
			if (data_len < len + 4)
				goto Read;
			//printf("len = %d\n", len);
			//fwrite(&len, 4, 1, ts);

			DATA_SKIP(4);
			DATA_SKIP(rtp);
            len -= rtp;
			buf = (u_char *)(data_buf + data_off);
			for (i = 0; i < len; i += 188) {
			    if (buf[i] != 0x47)
			        printf("not sync!\n");
			}
			fwrite(data_buf + data_off, len, 1, ts);
			DATA_SKIP(len);
		}
	}
Err:
	if (fp)
		fclose(fp);
	if (ts)
		fclose(ts);
	if (data_buf)
		free(data_buf);

	// getch();

	return 0;
}
